home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Text / WASTE / WASTE 1.2a2 / WEUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-12  |  1.9 KB  |  107 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEUtilities.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Miscellaneous General-Purpose Utilities
  6.  *
  7.  *  Copyright (c) 1993-1995 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. #ifndef __MEMORY__
  18. #include <Memory.h>
  19. #endif
  20.  
  21. pascal Boolean _WEBlockCmp(const void *block1, const void *block2, register long blockSize)
  22. {
  23.     register const char *p1 = (const char *) block1;
  24.     register const char *p2 = (const char *) block2;
  25.  
  26.     while ( --blockSize >= 0 )
  27.         if ( *p1++ != *p2++ )
  28.             return false;
  29.     
  30.     return true;
  31. }
  32.  
  33. pascal void _WEBlockClr(void *block, register long blockSize)
  34. {
  35.     register char *p = (char *) block;
  36.  
  37.     while ( --blockSize >= 0 )
  38.         *p++ = 0;
  39. }
  40.  
  41. pascal void _WEForgetHandle(Handle *h)
  42. {
  43.     Handle theHandle;
  44.     
  45.     if ((theHandle = *h) != NULL)
  46.     {
  47.         *h = NULL;
  48.         DisposeHandle(theHandle);
  49.     }
  50. }
  51.  
  52. pascal Boolean _WESetHandleLock(Handle h, Boolean lock)
  53. {
  54.     Boolean oldLock = (HGetState(h) & (1 << 7)) != 0;
  55.     
  56.     if (lock != oldLock)
  57.         if (lock)
  58.             HLock(h);
  59.         else
  60.             HUnlock(h);
  61.             
  62.     return oldLock;
  63. }
  64.  
  65. pascal void _WEReorder(long *a, long *b)
  66. {
  67.     if (*a > *b)
  68.     {
  69.         long temp = *a;
  70.         *a = *b;
  71.         *b = temp;
  72.     }
  73. }
  74.  
  75. pascal OSErr _WEAllocate(long blockSize, short allocFlags, Handle *h)
  76. {
  77.     // Allocate a new relocatable block.
  78.     // AllocFlags may specify whether the block should be cleared and whether
  79.     // temporary memory should be used.
  80.  
  81.     Handle theHandle = NULL;
  82.     OSErr err;
  83.  
  84.     // if kAllocTemp is specified, try tapping temporary memory
  85.     if (allocFlags & kAllocTemp)
  86.     {
  87.         theHandle = TempNewHandle(blockSize, &err);
  88.     }
  89.  
  90.     // if kAllocTemp isn't specified, or TempNewHandle failed, try with current heap
  91.     if (theHandle == NULL) 
  92.     {
  93.         theHandle = NewHandle(blockSize);
  94.         err = MemError();
  95.     }
  96.     
  97.     // if kAllocClear is specified, zero the block
  98.     if ((allocFlags & kAllocClear) && (theHandle != NULL))
  99.     {
  100.         _WEBlockClr(*theHandle, blockSize);
  101.     }
  102.  
  103.     // return handle through VAR parameter
  104.     *h = theHandle;    
  105.     return err;
  106. }
  107.